package com.dhwl.hsh.merchant.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.dhwl.hsh.lib.bean.Address;
import com.dhwl.hsh.lib.bean.resp.OrderDetailResp;
import com.dhwl.hsh.lib.core.HshException;
import com.dhwl.hsh.lib.utils.DateUtil;
import com.dhwl.hsh.lib.utils.StringUtils;
import com.dhwl.hsh.lib.utils.SystemTool;
import com.dhwl.hsh.merchant.R;
import com.dhwl.hsh.merchant.common.Constant;
import com.dhwl.hsh.merchant.presenter.OrderDetailPresenter;
import com.dhwl.hsh.merchant.presenter.iview.IOrderDetailView;
import com.dhwl.hsh.merchant.ui.adapter.ShipingAddressAdapter;
import com.dhwl.hsh.merchant.ui.dialog.AlertDialogFragment;
import com.dhwl.hsh.merchant.view.ScrollViewWithListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 订单详情
 *
 * @author Derek.Kidd
 * @time 2016/4/26 18:34
 */
public class OrderDetailActivity extends BaseActivity implements IOrderDetailView {

    @Bind(R.id.tv_order_num)
    TextView tvOrderNum;
    @Bind(R.id.tv_order_type)
    TextView tvOrderType;
    @Bind(R.id.tv_order_status)
    TextView tvOrderStatus;
    @Bind(R.id.tv_order_time)
    TextView tvOrderTime;
    @Bind(R.id.tv_order_driver_type)
    TextView tvOrderDriverType;
    @Bind(R.id.tv_order_mileage)
    TextView tvOrderMileage;
    @Bind(R.id.tv_order_start_add)
    TextView tvStartAdd;
    @Bind(R.id.img_order_shipping_call)
    ImageView imgOrderShippingCall;
    @Bind(R.id.scrollView_content)
    ScrollView scrollViewContent;
    @Bind(R.id.listView_address)
    ScrollViewWithListView listViewAddress;
    @Bind(R.id.btn_need_carry)
    Button btnNeedCarry;
    @Bind(R.id.btn_need_receipt)
    Button btnNeedReceipt;
    @Bind(R.id.btn_need_remittance)
    Button btnNeedRemittance;
    @Bind(R.id.layout_order_toast)
    LinearLayout layoutOrderToast;
    @Bind(R.id.tv_choose_truck)
    TextView tvChooseTruck;
    @Bind(R.id.img_order_detail_call_driver)
    ImageView imgOrderDetailCallDriver;
    @Bind(R.id.tv_driver_rate)
    TextView tvDriverRate;
    @Bind(R.id.tv_order_words)
    TextView tvOrderWords;
    @Bind(R.id.layout_driver)
    RelativeLayout layoutDriver;
    @Bind(R.id.layout_driver_grade)
    RelativeLayout layoutDriverGrade;
    @Bind(R.id.tv_start_mileage)
    TextView tvStartMileage;
    @Bind(R.id.tv_ship_start_price)
    TextView tvShipStartPrice;
    @Bind(R.id.tv_more_mileage)
    TextView tvMoreMileage;
    @Bind(R.id.tv_ship_more_price)
    TextView tvShipMorePrice;
    @Bind(R.id.tv_order_total_price)
    TextView tvOrderTotalPrice;
    @Bind(R.id.layout_bill_mileage)
    LinearLayout layoutBillMileage;
    @Bind(R.id.tv_order_bill)
    TextView tvOrderBill;
    @Bind(R.id.tv_order_cost_type)
    TextView tvCostType;

    @Bind(R.id.layout_to_task)
    RelativeLayout layoutToTask;


    @Bind(R.id.layout_bottom_action)
    RelativeLayout bottomLayout;
    @Bind(R.id.btn_cancel_order)
    Button btnCancelOrder;


    OrderDetailPresenter orderDetailPresenter;

    OrderDetailResp orderDetailResp;

    ShipingAddressAdapter addressAdapter;
    /**
     * 配送地列表数据
     */
    List<Address> addressList;

    String orderId;

    int costType;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_order_detail;
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        setTitleContent("订单详情");
        listViewAddress.setParentScrollView(scrollViewContent);
    }

    @Override
    protected void initData() {
        orderId = getIntent().getStringExtra(Constant.Order.KEY_ORDER_ID);
        orderDetailPresenter = new OrderDetailPresenter();
        orderDetailPresenter.attachView(this);
        orderDetailPresenter.getOrderDetail(orderId);

        addressList = new ArrayList<>();
        addressAdapter = new ShipingAddressAdapter(this, addressList);
    }

    @OnClick(R.id.layout_to_task)
    public void onTaskClick(View view) {
        Intent intent = new Intent();
        intent.setClass(this, TaskDetailActivity.class);
        startActivity(intent);
    }


    @Override
    public void onOrderDetailSuccess(OrderDetailResp orderDetailResp) {
        this.orderDetailResp = orderDetailResp;

        //订单号
        tvOrderNum.setText(String.format(getString(R.string.order_num), orderDetailResp.getOrderId()));
        // 订单类型
        if (Constant.Order.ORDER_TYPE_ONCE == orderDetailResp.getOrderType()) {
            tvOrderType.setText(String.format(getString(R.string.order_type), getString(R.string.once)));
        } else if (Constant.Order.ORDER_TYPE_CYCLE == orderDetailResp.getOrderType()) {
            tvOrderType.setText(String.format(getString(R.string.order_type), getString(R.string.cycle)));
        } else {
            tvOrderType.setText(String.format(getString(R.string.order_type), getString(R.string.cycle)));
        }
        //用车时间
        if (!TextUtils.isEmpty(orderDetailResp.getUseCarTime())) {
            tvOrderTime.setText(String.format(getString(R.string.order_transport_time), DateUtil.getDate(Long.valueOf(orderDetailResp.getUseCarTime()), DateUtil.FORMAT_DATE_YYYYMMDD_T_HHMM)));
        } else {
            tvOrderTime.setText(String.format(getString(R.string.order_transport_time), ""));
        }
        //车型
        if (!TextUtils.isEmpty(orderDetailResp.getCarTypeName()))
            tvOrderDriverType.setText(String.format(getString(R.string.order_car_type), orderDetailResp.getCarTypeName()));
        //订单状态 TODO: 16/6/3
        tvOrderStatus.setText(orderDetailResp.getOrderState() + "");
        // FIXME: 16/6/3  总里程数
        tvOrderMileage.setText(String.format(getString(R.string.order_total_mileage), 40.0));//总里程数
        //发货地
        List<Address> orderDestination = orderDetailResp.getOrderDestination();
        tvStartAdd.setText(orderDestination.get(0).getPosiName());
        //配送地
        orderDestination.remove(0);
        addressAdapter.addAll(orderDestination);
        listViewAddress.setAdapter(addressAdapter);
        // 备注:
        String condition = orderDetailResp.getJobCondition();
        String str[] = condition.split("-");
        for (int i = 0; i < str.length; i++) {
            if (TextUtils.equals(str[i], Constant.Order.VALUE_CONDITION_CAR)) {
                //搬运
                btnNeedCarry.setTextColor(getResources().getColor(R.color.color_ff9b1a));
                btnNeedCarry.setBackgroundResource(R.drawable.bg_extra_service_checked);
            } else if (TextUtils.equals(str[i], Constant.Order.VALUE_CONDITION_PAY)) {
                //回款
                btnNeedReceipt.setTextColor(getResources().getColor(R.color.color_ff9b1a));
                btnNeedReceipt.setBackgroundResource(R.drawable.bg_extra_service_checked);
            } else if (TextUtils.equals(str[i], Constant.Order.VALUE_CONDITION_REC)) {
                //回单
                btnNeedRemittance.setTextColor(getResources().getColor(R.color.color_ff9b1a));
                btnNeedRemittance.setBackgroundResource(R.drawable.bg_extra_service_checked);
            }
        }
        //捎句话
        tvOrderWords.setText(orderDetailResp.getWords());
        //配送司机
        tvChooseTruck.setText(orderDetailResp.getOrderAcceptUser());
        //配送司机电话
        imgOrderDetailCallDriver.setTag(orderDetailResp.getDiverMobile());
        //司机评分
        tvDriverRate.setText(String.format(getString(R.string.order_user_rate), orderDetailResp.getDriverScore()));
        //计费方式
        costType = orderDetailResp.getCostType();
        switch (costType) {
            case Constant.Order.VALUE_BILL_TYPE_1:
                tvCostType.setText(R.string.tv_bill_type_1);
                break;
            case Constant.Order.VALUE_BILL_TYPE_2:
                tvCostType.setText(R.string.tv_bill_type_2);
                break;
            case Constant.Order.VALUE_BILL_TYPE_3:
                tvCostType.setText(R.string.tv_bill_type_1);
                break;
            case Constant.Order.VALUE_BILL_TYPE_4:
                tvCostType.setText(R.string.tv_bill_type_1);
                break;
        }

        //按里程计费:起步公里数
        tvStartMileage.setText(String.format(getString(R.string.order_start_price), orderDetailResp.getParam1()));


//          param5;//按趟计费：每趟配送货物运费
//          param6;//货价计费：整车货物价格
//          param7;//货价计费：运费比例
//          param8;//按数量计费：整车货物件数
//          param9;//按数量计费：每件运费

        //按里程计费:起步公里数价格
        tvShipStartPrice.setText(StringUtils.getPrice(orderDetailResp.getParam2()));
        //按里程计费:超公里数
        tvMoreMileage.setText(String.format(getString(R.string.order_more_mileage), orderDetailResp.getParam3()));
        //按里程计费:超公里数价格
        tvShipMorePrice.setText(StringUtils.getPrice(orderDetailResp.getParam4()));

        //运费总价
        tvOrderTotalPrice.setText(StringUtils.getPrice(orderDetailResp.getCostMoney()));
        //小费
        tvOrderBill.setText(orderDetailResp.getBillMoney());

        //是否显示"取消订单"
        if (orderDetailResp.isOverOrder()) {
            bottomLayout.setVisibility(View.VISIBLE);
        } else {
            bottomLayout.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    public void onCancelOrderSuc() {
        finish();
    }

    @Override
    public void onOrderDetailFailure(HshException e) {

    }

    @Override
    public void onCancelOrderFalure(HshException e) {

    }


    @OnClick(R.id.img_order_shipping_call)
    void onCallClick() {
        SystemTool.call(this, orderDetailResp.getOrderDestination().get(0).getLinkManMobile());
    }

    @OnClick(R.id.btn_cancel_order)
    void onCancelOrderClick(View view) {
        AlertDialogFragment alertDialogFragment = AlertDialogFragment.newInstance(getString(R.string.dialog_title_cancel_order), getString(R.string.alert_cancel_order));

        alertDialogFragment.setOnDialogSureClick
                (new AlertDialogFragment.OnSureClickListener() {
                    @Override
                    public void onDialogSureClick() {
                        //1:商户取消订单
                        orderDetailPresenter.cancelOrder(1, orderDetailResp.getOrderId());
                    }
                });
        alertDialogFragment.show(getFragmentManager(), "order_cancel_dialog");
    }

}
